package com.cgs.query.handler.result

import java.sql.ResultSet
import java.sql.ResultSetMetaData

class MapResultSetHandler : ListResultSetHandler() {

    override fun mapRow(colCount: Int, rs: ResultSet): Any {
        val listMap = linkedMapOf<String, Any?>()
        val metaData = getMetaData(rs.metaData)
        for (i in 1..colCount) {
            listMap[metaData[i - 1]] = rs.getObject(i)
        }
        return listMap
    }

    private fun getMetaData(metaData: ResultSetMetaData): List<String> {
        val array = arrayListOf<String>()
        val colCount = metaData.columnCount
        for (i in 1..colCount) {
            array.add(metaData.getColumnName(i))
        }
        return array
    }
}